package com.adlotto2.lcdns.activity.mypage.adapter;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.adlotto2.lcdns.R;
import com.adlotto2.lcdns.cronos.data.CNLottoAdMyPageData;
import com.adlotto2.lcdns.cronos.datahandler.CNBaseDataSet;
import com.adlotto2.lcdns.util.RecycleUtils;

public class LottoAdMyPageAdapter extends BaseAdapter{

	protected LayoutInflater mInflater = null;
	protected ArrayList<CNBaseDataSet> mDataSetList = null;
	
	private List<WeakReference<View>> mRecycleList = new ArrayList<WeakReference<View>>();
	
	public void recycle() {
		for (WeakReference<View> ref : mRecycleList) {
			RecycleUtils.recursiveRecycle(ref.get());
		}
	}
	
	private class ViewHolder {
		TextView mChaTextView;
		TextView mDateTextView;
		TextView mLottoNumTextview;
		TextView mRealMoneyTextview;
		TextView mRankTextView;
	}
	
	public LottoAdMyPageAdapter(Context context){
		this.mInflater = LayoutInflater.from(context);
	}
	
	public void setDataSetList(ArrayList<CNBaseDataSet> dataSetList) {
		this.mDataSetList = dataSetList;
	}
	
	public void clear(){
		if(this.mDataSetList != null){
			this.mDataSetList.clear();
			this.notifyDataSetChanged();
		}
	}
	
	@Override
	public int getCount() {
		if (this.mDataSetList != null) {
			return this.mDataSetList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (this.mDataSetList != null) {
			return this.mDataSetList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null){
			convertView = this.mInflater.inflate(R.layout.lottoad_menu_mypage_row, null);
			holder = new ViewHolder();
			
			holder.mChaTextView = (TextView) convertView.findViewById(R.id.textView_cha);
			holder.mLottoNumTextview = (TextView) convertView.findViewById(R.id.textView_lottoNum);
			holder.mRealMoneyTextview = (TextView) convertView.findViewById(R.id.textView_money);
			holder.mRankTextView = (TextView) convertView.findViewById(R.id.textView_rank);
			holder.mDateTextView = (TextView) convertView.findViewById(R.id.textView_date);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		CNLottoAdMyPageData dataSet = (CNLottoAdMyPageData) getItem(position);
		
		if(dataSet != null){
			
			holder.mChaTextView.setText(String.valueOf(dataSet.chaSeq));
			
			holder.mDateTextView.setText(dataSet.recDate.split(" ")[0].substring(2, dataSet.recDate.split(" ")[0].length()));
			holder.mDateTextView.setPaintFlags(holder.mDateTextView.getPaintFlags() | Paint.FAKE_BOLD_TEXT_FLAG);
			
			holder.mLottoNumTextview.setText(dataSet.lottoNum);
			holder.mRealMoneyTextview.setText(dataSet.realMoney);
			if(dataSet.rank.equals("0")){
				holder.mRankTextView.setText("X");
			} else {
				holder.mRankTextView.setText(dataSet.rank + " 등");
			}
		}
		
		return convertView;
	}
}
